//
//  RepairVCViewController.m
//  QoilPublic
//
//  Created by SunChao on 2018/4/19.
//  Copyright © 2018年 孙超. All rights reserved.
//

#import "RepairVC.h"
#import "LMJDropdownMenu.h"
#import "SINavigationMenuView.h"
#import "AddDeviceVC.h"
#import "TotilRepairCell.h"
#import "InstructionsCell.h"
#import "RepairDetailVC.h"
@interface RepairVC ()<LMJDropdownMenuDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)SINavigationMenuView *menu;
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,copy)NSString *totalMoney;

@end

@implementation RepairVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
    
    but.frame =CGRectMake(0,0, 60, 44);
    
    [but setTitle:@"新增"forState:UIControlStateNormal];
    [but setFont:[UIFont systemFontOfSize:14]];
    [but addTarget:self action:@selector(PushToAddDevice)forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem  *barBut = [[UIBarButtonItem alloc]initWithCustomView:but];
    
    self.navigationItem.rightBarButtonItem = barBut;
    
    [self.tbView registerNib:[UINib nibWithNibName:@"TotilRepairCell" bundle:nil] forCellReuseIdentifier:@"TotilRepairCell"];
     [self.tbView registerNib:[UINib nibWithNibName:@"InstructionsCell" bundle:nil] forCellReuseIdentifier:@"InstructionsCell"];
    LMJDropdownMenu * dropdownMenu = [[LMJDropdownMenu alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 35) title:@"2018年4月"];
    [dropdownMenu setMenuTitles:@[@"2018年3月",@"2018年2月",@"2018年1月"] rowHeight:30];
    dropdownMenu.delegate = self;
    [self.view addSubview:dropdownMenu];
    self.tbView.frame = CGRectMake(0, 100, kScreenWidth, kScreenHeight);
    self.tbView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.view addSubview:self.tbView];
    [self requestData];
}

- (void)PushToAddDevice{
    
    
    AddDeviceVC *vc = [[AddDeviceVC alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count+2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        return  60;
    }
    if (indexPath.row == 1) {
        return 35;
    }
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        TotilRepairCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TotilRepairCell"];
        cell.totalMoney.text = [NSString stringWithFormat:@"%@", self.totalMoney];
        cell.userInteractionEnabled = NO;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    InstructionsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InstructionsCell"];
    if (indexPath.row == 1) {
        cell.userInteractionEnabled = NO;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
        NSDictionary *tmpDic =  [self.dataSource objectAtIndex:indexPath.row-2];
        [cell setCellWithData:tmpDic];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"indexPaht:%ld",indexPath.row);
    RepairDetailVC *vc = [[RepairDetailVC alloc]init];
    vc.title = @"斗山210";
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(UITableView *)tbView{
    
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tbView.delegate = self;
        _tbView.dataSource = self;
        _tbView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _tbView;
    
}
#pragma mark - LMJDropdownMenu Delegate

- (void)dropdownMenu:(LMJDropdownMenu *)menu selectedCellNumber:(NSInteger)number{
    NSLog(@"你选择了：%ld",number);
}

- (void)dropdownMenuWillShow:(LMJDropdownMenu *)menu{
    NSLog(@"--将要显示--");
}
- (void)dropdownMenuDidShow:(LMJDropdownMenu *)menu{
    NSLog(@"--已经显示--");
}

- (void)dropdownMenuWillHidden:(LMJDropdownMenu *)menu{
    NSLog(@"--将要隐藏--");
}
- (void)dropdownMenuDidHidden:(LMJDropdownMenu *)menu{
    NSLog(@"--已经隐藏--");
}
//http://192.168.2.102:8081/maintenance/list

- (void)requestData{
   
    NSString *url = [QParamService urlWithIOTAction:@"/maintenance/list"];
   
    NSString *token = [QParamService getToken];
    NSDictionary *param = @{
                                   @"token":token
                                   };
    [[QHttpManager sharedManager] POST:url parameterMD5jm:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if (error) {
            
            NSLog(@"error:%@",basModel.errMsg);
            
            return ;
        }
        
        if([basModel.result isEqualToString:@"success"]){
            
            NSLog(@"result:%@",basModel.result);
            NSArray *array = basModel.items;
            NSDictionary *dic = [array objectAtIndex:0];
            self.dataSource = [dict objectForKey:@"list"];
            self.totalMoney = [dic objectForKey:@"totalMoney"];
        }
        [self.tbView reloadData];
    }];
}
ARRAY_INIT(dataSource);

@end
